Hexo 中利用 Mathjax 书写公式

科技文章的写作过程中会碰到许许多多的公式需要书写。这个问题困扰了我很久。在本地有MathType等一系列软件可以使用,Latex也有良好的本地支持,但是当我想在网上建立起自己的个人知识管理系统时却遇到了比较多的问题。这里做的是用Hexo + MathJax 录入数学公式的尝试。具体的讲解与方法参见sunxb10的这篇博文


Next主题修改

每个主题对mathjax的修改方式不尽相同。我也是在多次尝试以后找到了针对Next主题的修改方式。(Next主题默认的mathjax不支持公式编号,所以我才对其进一步进行修改)。

用listary查找mathjax的文件

首先是在主题目录找到mathjax的文件。这里推荐用listary这款软件,在主题目录文件夹中按listary搜索快捷键(默认是windows+~好像,安装listary时一般会提醒设置)。

利用listary查找mathjax文件

在Next主题中,我们发现mathjax的相关配置在mathjax.swig文件中,将其打开。

修改文件的代码

将上述引用的博客中的代码替换到自己主题的mathjax文件的适当位置。我这里是将那一串代码粘贴替换掉了mathjax.swig中原来的标签中的内容,修改完成后保存即可。

1
{% if theme.mathjax %}
<!-- MathJax Start -->
<!-- MathJax documentation: http://docs.mathjax.org/en/latest/index.html -->
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {  // tex2jax preprocessor
      inlineMath: [ ['$','$'] ],  // delimiters for in-line math
      displayMath: [ ['$$','$$'] ],  // delimiters for displayed equations
      processEscapes: true,  // enable \$ to represent a single dollar sign
      skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']  // MathJax will not process contents inside these tags 
    },
    TeX: {  // TeX/LaTeX input processor
      equationNumbers: { autoNumber: "AMS" },  // only number those equations in specific AMSmath environments
      extensions: ["AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js"]  // introduce AMS extensions and suppress generating error messages 
    },
    "HTML-CSS": {  // HTML-CSS output processor (this is the default output of MathJax)
      scale: 110,  // The scaling factor of math with respect to the surrounding text
      linebreaks: { automatic: true } // automatically breaks the line if necessary
    },
    SVG: {  // SVG output processor
      scale: 110,  // The scaling factor of math with respect to the surrounding text
      linebreaks: { automatic: true } // automatically breaks the line if necessary
    },
    menuSettings: {  // settings for the mathematics contextual menu
      zoom: "Hover"  // set equation zooming to be triggered by a single mouse click
    }
  });
 
  MathJax.Hub.Queue(function() { // Fix <code> tags after MathJax finishes running, this is a hack to overcome a shortcoming of Markdown
      var all = MathJax.Hub.getAllJax(), i;
      for(i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>
<script type="text/javascript"
   src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">  // link to the MathJax CDN
</script>
<!-- MathJax End -->

<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
{% endif %}

效果测试

$E = m c^{2}$

$a^{2} + b^{2} = c^{2}$

$$f(x) = \frac{1}{\sigma\sqrt{2\pi}}e^{-\frac{(x-\mu)^{2}}{2\sigma^{2}}}$$

$$v=\frac{\Delta s}{\Delta t}$$

$$f{n}=f{n-1}+f_{n-2}$$

$$f_{n}=f_{n-1}+f_{n-2}$$

$$
\mathrm{e}^{i\theta}=\cos\theta+i\sin\theta\
\mathrm{e}^{i\pi}+1=0
$$

$$
\mathrm{e}^{i\theta}=\cos\theta+i\sin\theta\\
\mathrm{e}^{i\pi}+1=0
$$

\begin{equation}
f_{0} = 1\\
f_{1} = 1\\
f_{n} = f_{n-1} + f_{n-2}\quad (n>1)
\end{equation}

\begin{align}
\begin{vmatrix}
1 & 1 & 1 & \cdots & 1 \\
a_{1} & a_{2} & a_{3} & \cdots & a_{n} \\
a_{1}^{2} & a_{2}^{2} & a_{3}^{2} & \cdots & a_{n}^{2} \\
a_{1}^{3} & a_{2}^{3} & a_{3}^{3} & \cdots & a_{n}^{3} \\
\vdots & \vdots & \vdots & \ddots & \vdots \\
a_{1}^{n−1} & a_{2}^{n−1} & a_{3}^{n−1} & \cdots & a_{n}^{n−1}
\end{vmatrix} = \prod_{1\leqslant{}j < i\leqslant{}n}(a_{i}−a_{j})
\end{align}

\begin{align}
&e^{i\pi} + 1 = 0 \label{eq:Euler}\\
&\hat{H}\Psi = i\hbar\frac{\partial}{\partial t}\Psi \label{eq:Schrödinger}\\
&\sqrt{1 + 2\sqrt{1 + 3\sqrt{1 + 4\sqrt{1 + \cdots}}}} = 3 \label{eq:Ramanujan}
\end{align}

\eqref{eq:Euler}式是欧拉公式
\eqref{eq:Schrödinger}式是薛定谔方程
\eqref{eq:Ramanujan}式是拉马努金恒等式

\begin{align*}
&\nabla \cdot \boldsymbol{E} = \frac{\rho}{\varepsilon_{0}} \\
&\nabla \cdot \boldsymbol{B} = 0 \\
&\nabla \times \boldsymbol{E} = -\frac{\partial\boldsymbol{B}}{\partial t} \\
&\nabla \times \boldsymbol{B} = \mu_{0} \boldsymbol{J} + \mu_{0}\varepsilon_{0}\frac{\partial\boldsymbol{E}}{\partial t}
\end{align*}

\begin{align}
E &= mc^{2} \notag \\
E &= h\nu \notag \\
m &= h\nu/c^{2}
\end{align}